package com.recommend.recommendkeyword.service.impl;

import com.recommend.recommendkeyword.entity.Keywords;
import com.recommend.recommendkeyword.entity.Tag;
import com.recommend.recommendkeyword.entity.TagToKeyword;
import com.recommend.recommendkeyword.mapper.KeyWordsMapper;
import com.recommend.recommendkeyword.mapper.RecommendKeywordMapper;
import com.recommend.recommendkeyword.mapper.TagMapper;
import com.recommend.recommendkeyword.mapper.TagToKeywordMapper;
import com.recommend.recommendkeyword.service.TagService;
import com.recommend.recommendkeyword.vo.TagVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private TagToKeywordMapper tagToKeywordMapper;
    @Autowired
    private KeyWordsMapper keyWordsMapper;
    @Autowired
    private RecommendKeywordMapper recommendKeywordMapper;
    /**
     * 根据标签获取关键词列表
     * @return List<Keywords>
     */
    @Override
    public List<TagVo> getKeywordListByTag() {
        List<TagToKeyword> tagToKeywords = tagToKeywordMapper.getTagToKeyword();
        List<TagVo> tagVos = new ArrayList<>();
        for (TagToKeyword tagToKeyword : tagToKeywords){
            TagVo tagVo = new TagVo();
            Keywords keywords = keyWordsMapper.getKeywordById(tagToKeyword.getKeywordId());
            Tag tag = tagMapper.getTagById(tagToKeyword.getTagId());
            String [] maxKeyword = findHighestCompetitiveness(keywords.getRecommendIdList(), keywords.getCompetitiveness()).split("/");
            String recommend = recommendKeywordMapper.getWordById(Integer.parseInt(maxKeyword[0])).getRecommendName();
            tagVo.setRecommend(recommend);
            tagVo.setCompetitiveness(maxKeyword[1]);
            tagVo.setKeywords(keywords);
            tagVo.setTag(tag);
            tagVos.add(tagVo);
        }
        return tagVos;
    }
    public static String findHighestCompetitiveness(String recommendIdList, String competitiveness) {
        // 将推荐词和竞争度字符串拆分为数组
        String[] words = recommendIdList.split("/");
        String[] scores = competitiveness.split("/");

        // 校验长度是否一致
        if (words.length != scores.length) {
            throw new IllegalArgumentException("竞争词和竞争度的数量不一致！");
        }

        // 初始化最大竞争度及对应词
        String maxWord = "";
        double maxScore = Double.MIN_VALUE;

        // 遍历数组，寻找最大竞争度及对应词
        for (int i = 0; i < words.length; i++) {
            double score;
            try {
                score = Double.parseDouble(scores[i]);
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("竞争度必须为数字格式！错误值: " + scores[i]);
            }

            if (score > maxScore) {
                maxScore = score;
                maxWord = words[i];
            }
        }

        // 返回结果
        return maxWord + "/ " + maxScore;
    }
}
